import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:suxuanapp/erp/home/fresh_erp.dart';
import 'package:suxuanapp/web/js_bridge.dart';
import 'erp/home/erp_home.dart';
import 'examples/js.dart';
import 'examples/net/httpdemo.dart';
import 'examples/pullandpush.dart';
import 'web/js_bridge.dart';

class AppHomePage extends StatefulWidget {
  AppHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<AppHomePage>   with WidgetsBindingObserver {
  @override
  void initState() {
//    if(Platform.isAndroid){
//    }
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }
  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
  AppLifecycleState _lastLifecycleState;
  String life="此小部件未观察到任何生命周期更改";
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print("--" + state.toString());
    print("current state is $state");
      _lastLifecycleState = state;
      life="当前页面状态"+_lastLifecycleState.toString();

  setState(() {
    switch (state) {
    //切换前台时,可回调，初始化时，收不到回调
      case AppLifecycleState.resumed:
    //  var ss= js.context["browser"];
    //  if(ss!=null ){
    //    js.context["browser"].callMethod("setBrowser" ,["{\"showToolbar\": \"false\",\"fullScreen\": \"false\",\"autoCloseable\": \"false\",\"showRefresh\": \"false\" }"]);
    //  }
        break;
    //切换后台时,inactive，pause先后回调
      case AppLifecycleState.inactive:
        break;
      case AppLifecycleState.paused:
        break;
      case AppLifecycleState.detached:
        break;
    }
  });
  }


  @override
  Widget build(BuildContext context) {
//    js.context["locationSuccess"]=locationSuccess;
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(child:         Text(
              'JS 案例',
            ),onPressed: (){
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return JSHomePage( );
              }));
            },)
            ,

            RaisedButton(child:         Text(
              '网络Get&Post',
            ),onPressed: (){
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return NetHomePage( );
              }));
            },)
            ,
            RaisedButton(child:         Text(
              'ERP 案例',
            ),onPressed: (){
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return ERPPage();
              }));
            })          ,
            RaisedButton(child:         Text(
              '刷新与加载',
            ),onPressed: (){
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return FreshPage();

              }));
            })       ,
            RaisedButton(child:         Text(
              'ERP 刷新',
            ),onPressed: (){
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return FreshERPPage();
              }));
            })    ,

            RaisedButton(child:         Text(
              '生命周期',
            ),onPressed: (){
              if(runOnH5){
                print(   "运行在H5");
                showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        title: Text("运行在H5"),
                        content: Text("H5没有这东西"),
                        actions: <Widget>[
                          FlatButton(
                              onPressed: () {
                                Navigator.of(context).pop(this);
                              },
                              child: Text("关闭"))
                        ],
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.all(Radius.circular(5))),
                      );
                    });
              }else{
                Fluttertoast.showToast(msg: "生命周期" );
              }

//              Navigator.push(context, MaterialPageRoute(builder: (context) {
//                return LifecycleWatcher();
//              }));
            })

          ],
        ),
      ),
    );
  }

}
